package com.bentley.kotlindemo.net

import com.bentley.kotlindemo.net.interceptor.LoggerInterceptor
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * <pre>
 *  ┌────────────────────────────────────────────
 *  │ @author  Lee  @date 2021/3/16 14:21
 *  ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 *  │ @desc
 *  └────────────────────────────────────────────
 * </pre>
 */
object RetrofitBuilder {

    //    private const val BASE_URL = "https://5e510330f2c0d300147c034c.mockapi.io/"
//    private const val BASE_URL = "https://www.wanandroid.com/"
    private const val BASE_URL = "https://reqres.in/"

    private fun getRetrofit(): Retrofit {
        var httpLoggingInterceptor =
            HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
        var loggerInterceptor = LoggerInterceptor()

        val interceptor: Interceptor
//通用拦截
        interceptor = Interceptor { chain ->
            val request = chain.request()
                .newBuilder()
                .addHeader("Content_Type", "application/json")
                .addHeader("charset", "UTF-8")
                .addHeader("token", "")
                .build()
            chain.proceed(request)
        }

        //Okhttp对象
        var okHttpClient = OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .addInterceptor(interceptor)
            .addInterceptor(loggerInterceptor)
            .build()

        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()


        val a=2

    }

    val apiService: ApiService = getRetrofit().create(ApiService::class.java)

    val apiService1: ApiService = getRetrofit().create(ApiService::class.java)
}